//
//  JJAssetUploadManager.swift
//  JJAlbumFramework
//
//  Created by 吴梓杭 on 2025/10/17.
//

import UIKit
import Photos

@objc
public protocol JJAssetUploadManagerDelegate: NSObjectProtocol {
    
    
    /// 预加载相册资源
    /// - Parameter progress: 进度
    ///   - error: 错误信息
    @objc optional func preloadAsset(progress: Float, error: Error?)
    
    /// 获取相册资源
    /// - Parameters:
    ///   - albumLists: 获取所有相册列表
    ///   - mainAlbumListModel: 获取主相册
    ///   - assets: 相册资源
    ///   - waitUploadUpAssets: 过滤后未上传的相册资源
    /// - Returns: Void
    @objc optional func getAsset(albumLists: [JJAlbumListModel], mainAlbumListModel: JJAlbumListModel?, assets: [JJAssetModel], waitUploadUpAssets: [JJAssetModel])
    
    /// 当前上传相册资源
    /// - Parameters:
    ///   - asset: 当前上传相册资源
    ///   - isSuccess: 是否上传成功
    ///   - error: 上传失败错误信息
    @objc optional func assetUploadResult(currentAsset: JJAssetModel, isSuccess: Bool, error: Error?)
    
    /// 资源从iCloud下载进度
    /// - Parameters:
    ///   - currentAsset: 相册资源
    ///   - progress: 进度
    @objc optional func assetICloudDownloadProgress(currentAsset: JJAssetModel, progress: Double)
    
    /// 当前上传状态
    /// - Parameter isPaused: Bool
    @objc optional func currentUploadStatus(isPaused: Bool, allUploadCompleted: Bool)
    
}


public class JJAssetUploadManager {
    
    public static var single = JJAssetUploadManager()
    
    weak open var delegate: (any JJAssetUploadManagerDelegate)?
    
    /// 获取所有相册列表
    public var albumLists: [JJAlbumListModel] = []
    /// 获取主相册
    public var mainAlbumListModel: JJAlbumListModel?
    /// 相册资源
    public var assets: [JJAssetModel] = []
    /// 过滤后未上传的相册资源
    public var waitUploadUpAssets: [JJAssetModel] = []
    
    /// 获取相册资源
    /// - Parameter completion: 相册资源
    public func getAssets(_ completion: @escaping (_ albumLists: [JJAlbumListModel], _ mainAlbumListModel: JJAlbumListModel?, _ assets: [JJAssetModel], _ waitUploadUpAssets: [JJAssetModel]) -> Void) {
        internal_getAssets(completion)
    }
    
    // 开始上传资产
    public func startUpload() {
        internal_startUpload()
    }
    
    // 暂停上传
    public func pauseUpload() {
        internal_pauseUpload()
    }
    
    // 恢复上传
    public func resumeUpload() {
        internal_resumeUpload()
    }
    
    // 获取当前状态
    public func getStatus() {
        internal_getStatus()
    }

    // 取消所有操作
    public func cancelAll() {
        internal_cancelAll()
    }
    
    deinit {
        print(message: "SimpleUploader 被释放")
    }
    
}


public extension JJAssetUploadManager {
    
    
    // 获取相册资源列表
    func getResources(current: Int, size: Int, completion: ((JJResourceListModel?) -> Void)? = nil) {
        let task = JJNetworkTask.getFiles(current: current, size: size)
        task.request { response in
            let dict = response.json.dictionaryValue
            print("原始 JSON 字典:", dict) // 调试日志
            if let responseModel = JJResourceListModel.parse(dict) {
                print("解析后的模型:", responseModel.total) // 调试日志
                completion?(responseModel)
            }else {
                print("解析错误")
            }
        } failure: { response, error in
            completion?(nil)
        }
    }
    
    
    /// 删除资源
    /// - Parameters:
    ///   - fileId: 资源id
    ///   - completion: 删除回调
    func deleteResources(_ fileId: String, completion: ((Bool) -> Void)? = nil) {
        let task = JJNetworkTask.deleteFile(fileId)
        task.request { response in
            completion?(true)
        } failure: { response, error in
            completion?(false)
        }
    }
    
    
}
